var CommonUtil = {

    // 常量：往sessionStorage中存放的paramObj的key
    CONSTANT_PARAMOBJ: 'paramObj',

    /**
   * 注入页面之间跳转需要传递的参数
   * @paramObj 一个JSON对象，用来传递参数，其格式如下:
   *             {"articleId":"123456"}
   */
    injectParamObject: function (paramObj) {
        paramObj = paramObj || {};
        sessionStorage.setItem(this.CONSTANT_PARAMOBJ, JSON.stringify(paramObj));
    },

    /**
   * 取出存在sessionStorage中的参数对象
   * @return 一个JSON对象，其格式如下:
   *  {"articleId":"123456"}
   */
    takeOutParamObject: function () {
        var rt = sessionStorage.getItem(this.CONSTANT_PARAMOBJ);
        if (!rt) {
            return null;
        }

        return JSON.parse(rt);
    },

    /**
   * 移除sessionStorage中的参数对象
   */
    removeParamObject: function () {
        sessionStorage.removeItem(this.CONSTANT_PARAMOBJ);
    },

    /**
   * 判断数组
   * @param o
   * @returns {boolean}
   */
    isArray: function (o) {
        return Object.prototype.toString.call(o) === '[object Array]';
    },

    /**
   * 获取缩略图路径
   * @param path 原图路径
   * @param suffix 后缀，默认_200x200
   * @returns {string}
   */
    getSamllPicturePath: function (path, suffix) {
        if (!path) {
            return '';
        }

        if (!suffix) {
            suffix = '_200_200';
        }

        if (path.indexOf(suffix) !== -1) {
            return path;
        }

        var paths = path.split('.');
        paths[paths.length - 2] = paths[paths.length - 2] + suffix; // 取文件名
        return paths.join('.');
    },

    /**
   * 将时间串转化成Date对象
   * @param timeString yyyy-MM-dd[ HH:mm:ss]
   * @return {Date}
   */
    parseTime: function (timeString) {
        if (!timeString) {
            return new Date();
        }

        var dateArr = timeString.split(' ');
        var year = parseInt(dateArr[0].split('-')[0], 10);
        var month = parseInt(dateArr[0].split('-')[1], 10) - 1;
        var date = parseInt(dateArr[0].split('-')[2], 10);
        var hour = 0;
        var minute = 0;
        var seconds = 0;
        if (dateArr.length > 1) {
            hour = parseInt(dateArr[1].split(':')[0], 10);
            minute = parseInt(dateArr[1].split(':')[1], 10);
            seconds = parseInt(dateArr[1].split(':')[2], 10);
        }

        return new Date(year, month, date, hour, minute, seconds);
    },

    /**
   * 将对象参数转换成字符串参数a=b&c=d
   * @param obj
   * @returns {*}
   */
    parseObjtoParamStr: function (obj) {
        if (obj) {
            // 返回字符串，ajax请求参数为对象的时候会对key和value都encodeURIComponent
            var params = [];
            for (var attr in obj) {
                if (obj[attr] === '') {
                    continue;
                }
                else {
                    var param = attr + '=' + obj[attr];
                    params.push(param);
                }
            }

            return params.join('&');
        }

        return '';
    },

    /**
   * 获取浏览器参数
   * @param name
   * @returns {null}
   */
    getUrlParam: function (name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURI(r[2]);
        }

        return null;
    },

    /**
   * 读取cookie
   * @param cname cookie名字
   * @returns {*}
   */
    getCookie: function (cname) {
        var name = cname + '=';
        var decodedCookie = decodeURIComponent(document.cookie);
        var ca = decodedCookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1);
            }
            if (c.indexOf(name) == 0) {
                return c.substring(name.length, c.length);
            }

        }
        return '';
    },

    /**
   * 自动去除数字最后的0或小数点
   * @param num
   * @param digit 保留小数点位数，默认2位
   * @returns string
   */
    fixNumber: function (num, digit) {
        if (num === '' || isNaN(num)) {
            return num;
        }

        if (typeof digit === 'undefined') {
            digit = 2;
        }

        var result = parseFloat(num).toFixed(digit);
        while (result.charAt(result.length - 1) === '0') {
            result = result.substring(0, result.length - 1);
        }
        if (result.charAt(result.length - 1) === '.') {
            result = result.substring(0, result.length - 1);
        }

        return result;
    },
};
export default CommonUtil;
$(function () {
    // 点击清除按钮清空文本框内容
    $(document).on('blur ', 'input', function (e) {
        var $this = $(this);
        var temp_timer = setTimeout(function () {
            $this.siblings('.cdicon-ico_delete').hide();
            clearTimeout(temp_timer);
        }, 10);
    });
    $(document).on('focus input ', '.input-clear input', function () {
        $('input[required]').siblings('i').hide();
        if ($(this).val().length > 0) {
            $(this).siblings('i').show();
        }
        else {
            $(this).siblings('i').hide();
        }

    });
    $(document).on('click', '.input-clear>i', function () {
        $(this).siblings('input').val('');
    });
});
